package com.itheima.studentmanage.test;

import com.itheima.studentmanage.domain.Student;

import java.util.ArrayList;
import java.util.Scanner;

public class StudentManage {
    public static void main(String[] args) {
        Scanner sc=new Scanner(System.in);
        ArrayList<Student> list=new ArrayList<Student>();
        while(true){
            System.out.println("--------欢迎来到学生管理系统--------");
            System.out.println("1.添加学生");
            System.out.println("2.删除学生");
            System.out.println("3.修改学生");
            System.out.println("4.查看学生");
            System.out.println("5.退出");
            System.out.println("请输入您的选择:");
            int choice=sc.nextInt();
            switch(choice){
                case 1:
                    addStudent(list);
                    break;
                case 2:
                   // System.out.println("删除");
                    deleteStudent(list);
                    break;
                case 3:
                   // System.out.println("修改");
                    updateStudent(list);
                    break;
                case 4:
                   // System.out.println("查看");
                    queryStudent(list);
                    break;
                case 5:
                    System.out.println("感谢您的使用!");
                    return;

            }

        }

    }

    private static void updateStudent(ArrayList<Student> list) {
        System.out.println("请输入您要修改的学号:");
        Scanner scanner=new Scanner(System.in);
        String   sid=scanner.next();
        int index =getindex(list,sid);
        if (index==-1){
            System.out.println("您输入的学号不存在");
        }else{
          Student newstu=list.get(index);
          newstu.setSid(sid);
            System.out.println("请输入修改后的姓名");
            String name=scanner.next();
            newstu.setName(name);
            System.out.println("请输入修改后的年龄");
            int age=scanner.nextInt();
            newstu.setAge(age);
            System.out.println("请输入修改后的生日");
            String  birthday=scanner.next();
            newstu.setBirthday(birthday);

            System.out.println("修改成功!");
        }


    }

    private static void deleteStudent(ArrayList<Student> list) {
        System.out.println("请输入您要删除的学号:");
        Scanner sc =new Scanner(System.in);
        String   sid=sc.next();
        int index =getindex(list,sid);
        if(index==-1){
            System.out.println("您输入的学号不存在");
        }else{
            list.remove(index);
        }
        }





    private static void queryStudent(ArrayList<Student> list) {
        if (list.size()==0){
            System.out.println("无信息,请添加后重试!");
            return;
        }
        System.out.println("姓名\t学号\t年龄\t生日");
        for (int i = 0; i < list.size(); i++) {
            Student student=list.get(i);
            System.out.println(student.getName()+"\t"+student.getSid()+"\t"+student.getAge()+"\t"+student.getBirthday());

        }
    }

    public static int getindex(ArrayList<Student> list, String sid){
        int index=-1;
        for (int i = 0; i < list.size(); i++) {
           Student stu=list.get(i);
           if(sid.equals(stu.getSid())){
               index=i;
               return index;
           }

        }
        return index;

    }

    private static void addStudent(ArrayList<Student> list) {
        Scanner sc=new Scanner(System.in);
        System.out.println("请输入姓名");
        String name=sc.next();
        System.out.println("请输入学号:");
        String sid=sc.next();
        System.out.println("请输入年龄:");
        int age =sc.nextInt();
        System.out.println("请输入生日:");
        String birthday=sc.next();
        Student student=new Student(name,  age,  sid, birthday);
        list.add(student);
        System.out.println("添加成功");

    }
}
